%%
%实指数序列
%实现x(n)=(0.8)^n,0<=n<=10
n=0:10;
x=(0.8).^n;
stem(n,x);
title('实指数序列');

%%
%实现复指数序列x(n)=e^(0.4+0.6j)n，其中-1<=n<=10
clc;clear all
n0=-1;
n2=10;
n=n0:n2;
x=exp((0.4+0.6j)*n);
figure(1)
subplot(211)
stem(n,real(x),'.');
axis([-4,10,min(real(x))-1,1.2*max(real(x))])
title('复指数序列')
ylabel('实部');grid;
subplot(212)
stem(n,imag(x),'.');
axis([-4,10,min(imag(x))-1,1.2*max(imag(x))])
ylabel('虚部');xlabel('n');grid;

%%
%实现 x(n)=[1,2,3,4],求将它延拓5个周期所得到的序列
clc; clear all
x = [1, 2, 3, 4];
N = length(x);
k = 5;
nx = 0:N-1;
ny = 0:(k*N-1);
y = x(mod(ny, N) + 1);
figure(1)
subplot(211), stem(nx, x, '.');
axis([-1, N+1, 0, max(x)+1]); grid;
subplot(212), stem(ny, y, '.');
axis([-1, k*N, 0, max(y)+1]); grid;

%%
%已知两序列为x1(n)=[1,3,5,7,6,4,2,1],起始位置ns1=-3，x2(n)=[4,0,2,1,-1,3],起始位置ns2=1，求两序列的和ya以及乘积ym
clc; clear all
x1 = [1, 3, 5, 7, 6, 4, 2, 1];
x2 = [4, 0, 2, 1, -1, 3];
ns1 = -3;
ns2 = 1;
nf1=ns1+length(x1)-1;
nf2=ns2+length(x2)-1;
n1=ns1:nf1;
n2=ns2:nf2;
n=min(ns1,ns2):max(nf1,nf2);
y1=zeros(1,length(n));y2=y1;
y1(find((n>=ns1)&(n<=nf1)==1))=x1;
y2(find((n>=ns2)&(n<=nf2)==1))=x2;
ya=y1+y2;
ym=y1.*y2;
subplot(221);stem(n1,x1,'.');ylabel('x1(n)');grid;
subplot(223);stem(n2,x2,'.');xlabel('n');ylabel('x2(n)');grid;
subplot(222);stem(n,ya,'.');ylabel('y1(n)+y2(n)');grid;
subplot(224);stem(n,ym,'.');xlabel('n');ylabel('y1(n)*y2(n)');grid;


%求x(n)=[2,3,4,3,2]的DTFT，并患处它的幅频特性及相频特性
%%
% 方法1
clc;clear all
n=0:4;x=[2,3,4,3,2];
k=0:1000;w=(pi/500)*k;
X=x*(exp(-j*pi/500)).^(n'*k);
magX=abs(X);angX=angle(X);
subplot(221);stem(n,x,'.');
title('例2.32的序列图')
ylabel('x(n)');axis([0,5,0,6]);grid;
subplot(222);plot(w/pi,magX);grid;
xlabel('w/pi');ylabel('模值');title('幅频特性');
subplot(224);plot(w/pi,angX);grid;
xlabel('以\pi为单位的频率');ylabel('弧度');title('相频特性');

%%
% 方法2
n=0:4;a=[1];x=[2,3,4,3,2];
N=1000;
[X,w]=freqz(x,a,N,'whole');
magX=abs(X);angX=angle(X);
subplot(221);stem(n,x,'.');
title('例2.32的序列图')
ylabel('x(n)');axis([0,5,0,6]);grid;
subplot(222);plot(w/pi,magX);grid;
xlabel('w/pi');ylabel('模值');title('幅频特性');
subplot(224);plot(w/pi,angX);grid;
xlabel('以\pi为单位的频率');ylabel('弧度');title('相频特性');



